package com.bawei.module_mine.tengxun.mvi.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.mvi.base.BaseViewModel
import com.bawei.module_mine.tengxun.mvi.intent.LiveIntent
import com.bawei.module_mine.tengxun.mvi.model.entity.CreateLiveRoomEntity
import com.bawei.module_mine.tengxun.mvi.model.entity.SendRedPacketEntity
import com.bawei.module_mine.tengxun.mvi.model.repository.LiveRepository
import com.bawei.module_mine.tengxun.mvi.state.LiveState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch


class LiveViewModel : BaseViewModel<LiveIntent>(){
    private val repository: LiveRepository by  lazy { LiveRepository() }

    private var liveFlow = MutableStateFlow<LiveState>(LiveState.Init)
    private var deleteliveFlow = MutableStateFlow<LiveState>(LiveState.Init)
    private var sendRedPackliveFlow = MutableStateFlow<LiveState>(LiveState.Init)


    val liveFlow_ = liveFlow.asStateFlow()
    val deleteliveFlow_ = deleteliveFlow.asStateFlow()
    val sendRedPackliveFlow_ = sendRedPackliveFlow.asStateFlow()


    //1:根据意图发起网络请求
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is LiveIntent.createLiveRoom-> getSimpleTypeData(
                        createLiveRoom = it.createLiveRoom
                    )
                    is LiveIntent.deleteLiveRoom->DeleteLiveRoom(
                        roomId = it.roomId
                    )
                    is LiveIntent.sendRedPacket->sendRedPacket(
                        sendRedPacketEntity = it.sendRedPacketEntity
                    )
                }
            }
        }
    }
    //2:根据网络请求结果更新状态流
    private fun getSimpleTypeData(createLiveRoom: CreateLiveRoomEntity) {
        viewModelScope.launch {
            repository.getSimpleType(createLiveRoom)
            .catch { e->
                liveFlow.value = LiveState.Failed(e.message.toString())
            }.filter {
                it != null
            }.collect{
                if(it.code == 200){
                    if(it.data.push_url==null){
                        liveFlow.value = LiveState.Empty
                    }else{
                        liveFlow.value = LiveState.VideoSuccess(it.data)
                    }
                }else{
                    liveFlow.value = LiveState.Failed(it.msg)
                }
            }

        }
    }
    //删除直播间
    private fun DeleteLiveRoom(roomId:String) {
        viewModelScope.launch {
            repository.deleteLiveRoom(roomId)
                .catch { e->
                    deleteliveFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        if(it.data.push_url==null){
                            deleteliveFlow.value = LiveState.Empty
                        }else{
                            deleteliveFlow.value = LiveState.DeleteLiveSuccess(roomId)
                        }
                    }else{
                        deleteliveFlow.value = LiveState.Failed(it.msg)
                    }
                }

        }
    }
    //发送红包
    private fun sendRedPacket(sendRedPacketEntity: SendRedPacketEntity) {
        viewModelScope.launch {
            repository.sendRedPacket(sendRedPacketEntity)
                .catch { e->
                    sendRedPackliveFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        if(!it.data){
                            sendRedPackliveFlow.value = LiveState.Empty
                        }else{
                            sendRedPackliveFlow.value = LiveState.SendRedPacketSuccess(it.data)
                        }
                    }else{
                        sendRedPackliveFlow.value = LiveState.Failed(it.msg)
                    }
                }

        }
    }

}